package com.sinosoft.emplmis.jobless.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.emplmis.jobless.entity.JoblessInfoEntity;
import com.sinosoft.emplmis.jobless.service.JoblessInfoService;
import com.sinosoft.emplmis.jobless.web.dto.JoblessInfoData;
import com.sinosoft.emplmis.jobless.web.dto.JoblessSearchData;
import com.sinosoft.framework.core.common.exception.BusinessException;
import com.sinosoft.framework.core.common.utils.ResultJson;

/**
 * 失业人员动态信息管理
 * @author
 *
 */
@Controller
@RequestMapping("jobless/management")
public class JoblessManagerController {

	ObjectMapper mapper = new ObjectMapper();
	
    /**
     * 失业人员信息页面跳转
     * @return
     */
	@RequestMapping(value="/joblessManager", method = RequestMethod.GET)
	public String  JoblessManager(){
		return"jobless/joblessInfo";
	}
	
	/**
	 * 困难人员管理页面跳转
	 * @return
	 */
	@RequestMapping(value="/difficultyManager", method = RequestMethod.GET)
	public String  difficultEmployedInfoManager(){
		return"jobless/difficultyInfo";
	}
	
	/**
	 * 当年实现就业页面跳转
	 */
	@RequestMapping(value="/joblessReal", method = RequestMethod.GET)
	public String joblessReal(){
		return"jobless/joblessRealize";
	}
	
	/**
	 * 失业人员客观原因减少页面跳转
	 */
	@RequestMapping(value="/joblessReduce", method = RequestMethod.GET)
	public String ObjectiveReduce(){
		return"jobless/joblessReduce";
	}
	
	/**
	 * 放弃求职人员减少申报页面跳转
	 */
	@RequestMapping(value="/joblessGiveup", method = RequestMethod.GET)
	public String joblessGiveup(){
		return"jobless/joblessGiveup";
	}
	
//	/**
//	 * 失业人员服务台账页面跳转
//	 */
//	@RequestMapping(value="/ServiceMenu", method = RequestMethod.GET)
//	public String  JoblessService(){
//		return"jobless/joblessService";
//	}
//	
//	/**
//	 * 困难人员就业援助记录页面跳转
//	 */
//	@RequestMapping(value="/aidRecordMenu", method = RequestMethod.GET)
//	public String aidRecord(){
//		return"jobless/difficultyAidRecord";
//	}
//	
//	/**
//	 * 困难人员跟踪服务记录页面跳转
//	 */
//	@RequestMapping(value="/trackingRecordMenu", method = RequestMethod.GET)
//	public String trackingRecord(){
//		return"jobless/difficultyTrackingRecord";
//	}
//	
//	/**
//	 * 困难人员就业援助记录页面跳转
//	 */
//	@RequestMapping(value="/aidEffectMenu", method = RequestMethod.GET)
//	public String aidEffect(){
//		return"jobless/difficultyAidEffect";
//	}

	
	/**
	 * 失业人员信息服务类
	 */
	@Autowired
	private JoblessInfoService joblessInfoService;
	
	/**
	 * 获取失业人员基本信息
	 */
	@RequestMapping(value="/joblessInfoList",method=RequestMethod.POST)
	@ResponseBody
	public ResultJson getJoblessList(JoblessSearchData data){
		ResultJson result = new ResultJson();
		String success = "";
		String message = "";
		List<JoblessInfoData> resultList = new ArrayList<JoblessInfoData>();
		List<JoblessInfoEntity> joblessInfoEntityList = new ArrayList<JoblessInfoEntity>();
		try {
			joblessInfoEntityList = joblessInfoService.joblessInfoList(data);
			for (JoblessInfoEntity joblessInfoEntity : joblessInfoEntityList) {
				JoblessInfoData joblessInfoData = new JoblessInfoData();
				//失业人员实体类转化为失业人员dto
				joblessInfoData.toEntityData(joblessInfoData,joblessInfoEntity);
				resultList.add(joblessInfoData);
			}
			result.setTotal(joblessInfoService.count(data));
			result.setRows(resultList);
			success= ResultJson.SUCCESS_YES;
			message= ResultJson.MESSAGE;
		} catch (Exception e) {
			success= ResultJson.SUCCESS_NO;
			message= "获取失败";
			e.printStackTrace();
		}finally {
			result.setSuccess(success);
			result.setMessage(message);
		}
		return result;
	}
	
	/**
	 * 添加失业人员信息
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value="/insertJoblessInfo", method = RequestMethod.POST)
	@ResponseBody
	public String insertJobless (JoblessInfoData record){
		String result = "";
		ResultJson resultJson = new ResultJson();
		try {
			joblessInfoService.saveJoblessInfo(record);
		    resultJson.setSuccess(ResultJson.SUCCESS_YES);
		    resultJson.setMessage(ResultJson.MESSAGE);
		} catch (BusinessException be) {
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
			resultJson.setMessage(be.getMessage());
			be.printStackTrace();
		} catch (Exception e) {
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
			resultJson.setMessage("保存失败");
			e.printStackTrace();
		}finally{
			try {
				result = mapper.writeValueAsString(resultJson);
			}catch (IOException e) {
				e.printStackTrace();
			}
			return result;
		}		
	}
	
	/**
	 * 根据主键删除一条失业人员信息记录
	 * @param unemployedId
	 * @return
	 */
	@RequestMapping(value="/deleteJoblessInfo", method = RequestMethod.POST)
	@ResponseBody
	public String deleteJoblessInfo(@RequestParam("unemployedId") String Id){
		String result = "";
		ResultJson resultJson = new ResultJson(); 
		try {
			joblessInfoService.deleteByPrimaryKey(Id);
			resultJson.setSuccess(ResultJson.SUCCESS_YES);
			resultJson.setMessage(ResultJson.MESSAGE);
		} catch (Exception e) {
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
			resultJson.setMessage("删除失败");
			e.printStackTrace();
		}finally{
			try {
				result = mapper.writeValueAsString(resultJson);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	/**
	 * 回显失业人员信息记录
	 * @return
	 */
	@RequestMapping(value="/getJoblessInfo", method = RequestMethod.POST)
	@ResponseBody
	public JoblessInfoData getJoblessInfo(@RequestParam("unemployedId") String Id){
		JoblessInfoData joblessInfoData = new JoblessInfoData();
		JoblessInfoEntity joblessInfoEntity = joblessInfoService.selectByPrimaryKey(Id);
		joblessInfoData.toEntityData(joblessInfoData, joblessInfoEntity);
		return joblessInfoData;
	}
	
	/**
	 * 修改保存失业人员信息
	 * @return
	 */
	@RequestMapping(value="/updateJoblessInfo", method = RequestMethod.POST)
	@ResponseBody
	public String updateJoblessInfo(JoblessInfoData record){
		String result = "";
		ResultJson resultJson = new ResultJson();
		JoblessInfoEntity joblessInfoEntity = new JoblessInfoEntity();
		try {
			//将entity转化为实体类dto
			joblessInfoEntity.toEntityData(joblessInfoEntity, record, "");
			joblessInfoService.updateByPrimaryKey(joblessInfoEntity);
			resultJson.setSuccess(ResultJson.SUCCESS_YES);
			resultJson.setMessage(ResultJson.MESSAGE);
		} catch (Exception e) {
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
			resultJson.setMessage("修改失败");
			e.printStackTrace();
		}finally{
			try {
				result = mapper.writeValueAsString(resultJson);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	/**
	 * 修改保存放弃求职人员信息
	 * @return
	 */
	@RequestMapping(value="/updateJoblessGiveup", method = RequestMethod.POST)
	@ResponseBody
	public String updateJoblessGiveup(JoblessInfoData record){
		String result = "";
		ResultJson resultJson = new ResultJson();
		JoblessInfoEntity joblessInfoEntity = new JoblessInfoEntity();
		try {
			//将entity转化为实体类dto
			joblessInfoEntity.toEntityData(joblessInfoEntity, record, "");
			joblessInfoService.updateJoblessGiveup(joblessInfoEntity);
			resultJson.setSuccess(ResultJson.SUCCESS_YES);
			resultJson.setMessage(ResultJson.MESSAGE);
		} catch (Exception e) {
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
			resultJson.setMessage("修改失败");
			e.printStackTrace();
		}finally{
			try {
				result = mapper.writeValueAsString(resultJson);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	/**
	 * 修改保存困难人员信息
	 * @return
	 */
	@RequestMapping(value="/updateDifficulty", method = RequestMethod.POST)
	@ResponseBody
	public String updateDifficulty(JoblessInfoData record){
		String result = "";
		ResultJson resultJson = new ResultJson();
		JoblessInfoEntity joblessInfoEntity = new JoblessInfoEntity();
		try {
			//将entity转化为实体类dto
			joblessInfoEntity.toEntityData(joblessInfoEntity, record, "");
			joblessInfoService.updateDifficulty(joblessInfoEntity);
			resultJson.setSuccess(ResultJson.SUCCESS_YES);
			resultJson.setMessage(ResultJson.MESSAGE);
		} catch (Exception e) {
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
			resultJson.setMessage("修改失败");
			e.printStackTrace();
		}finally{
			try {
				result = mapper.writeValueAsString(resultJson);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	/**
	 * 获取困难人员信息
	 * @return
	 */
	@RequestMapping(value="/getDifficultyList", method = RequestMethod.POST)
	@ResponseBody
	public ResultJson getDifficultyInfoList(JoblessSearchData data){
		ResultJson result = new ResultJson();
		String success="";
		String message="";
		List<JoblessInfoData> resultList = new ArrayList<JoblessInfoData>();
		List<JoblessInfoEntity> joblessInfoEntityList = new ArrayList<JoblessInfoEntity>();
		try {
			joblessInfoEntityList = joblessInfoService.difficultyInfoList(data);
			for (JoblessInfoEntity joblessInfoEntity : joblessInfoEntityList) {
				JoblessInfoData joblessInfoData = new JoblessInfoData();
				//失业人员信息实体类转化为失业人员信息dto
				joblessInfoData.toEntityData(joblessInfoData,joblessInfoEntity);
				resultList.add(joblessInfoData);
			}
			int total =joblessInfoService.countDifficulty(data);
			result.setTotal(total);
			result.setRows(resultList);
			success= ResultJson.SUCCESS_YES;
			message= ResultJson.MESSAGE;
		} catch (Exception e) {
			success= ResultJson.SUCCESS_NO;
			message= "获取失败";
			e.printStackTrace();
		}finally {
			result.setSuccess(success);
			result.setMessage(message);
		}
		return result;
	}
	
	/**
	 * 获取困难人员信息
	 * @return
	 */
	@RequestMapping(value="/getHistoryDifficultyList", method = RequestMethod.POST)
	@ResponseBody
	public ResultJson getHistoryDifficultyInfoList(JoblessSearchData data){
		ResultJson result = new ResultJson();
		String success="";
		String message="";
		List<JoblessInfoData> resultList = new ArrayList<JoblessInfoData>();
		List<JoblessInfoEntity> joblessInfoEntityList = new ArrayList<JoblessInfoEntity>();
		try {
			joblessInfoEntityList = joblessInfoService.historyDifficultyInfoList(data);
			for (JoblessInfoEntity joblessInfoEntity : joblessInfoEntityList) {
				JoblessInfoData joblessInfoData = new JoblessInfoData();
				//失业人员信息实体类转化为失业人员信息dto
				joblessInfoData.toEntityData(joblessInfoData,joblessInfoEntity);
				resultList.add(joblessInfoData);
			}
			result.setTotal(joblessInfoService.countHistoryDifficulty(data));
			result.setRows(resultList);
			success= ResultJson.SUCCESS_YES;
			message= ResultJson.MESSAGE;
		} catch (Exception e) {
			success= ResultJson.SUCCESS_NO;
			message= "获取失败";
			e.printStackTrace();
		}finally {
			result.setSuccess(success);
			result.setMessage(message);
		}
		return result;
	}
	
	/**
	 * 获取当年实现就业人员信息
	 * @return
	 */
	@RequestMapping(value="/getJoblessRealize", method = RequestMethod.POST)
	@ResponseBody
	public ResultJson getJoblessRealizeList(JoblessSearchData data){
		ResultJson result = new ResultJson();
		String success="";
		String message="";
		List<JoblessInfoData> resultList = new ArrayList<JoblessInfoData>();
		List<JoblessInfoEntity> joblessInfoEntityList = new ArrayList<JoblessInfoEntity>();
		try {
			joblessInfoEntityList = joblessInfoService.joblessRealizeList(data);
			for (JoblessInfoEntity joblessInfoEntity : joblessInfoEntityList) {
				JoblessInfoData joblessInfoData = new JoblessInfoData();
				//失业人员信息实体类转化为失业人员信息dto
				joblessInfoData.toEntityData(joblessInfoData,joblessInfoEntity);
				resultList.add(joblessInfoData);
			}
			result.setTotal(joblessInfoService.countrealjob(data));
			result.setRows(resultList);
			success= ResultJson.SUCCESS_YES;
			message= ResultJson.MESSAGE;
		} catch (Exception e) {
			success= ResultJson.SUCCESS_NO;
			message= "获取失败";
			e.printStackTrace();
		}finally {
			result.setSuccess(success);
			result.setMessage(message);
		}
		return result;
	}
	
	/**
	 * 获取失业人员客观原因减少信息
	 * @return
	 */
	@RequestMapping(value="/getJoblessReduce", method = RequestMethod.POST)
	@ResponseBody
	public ResultJson getJoblessReduceList(JoblessSearchData data){
		ResultJson result = new ResultJson();
		String success="";
		String message="";
		List<JoblessInfoData> resultList = new ArrayList<JoblessInfoData>();
		List<JoblessInfoEntity> joblessInfoEntityList = new ArrayList<JoblessInfoEntity>();
		try {
			joblessInfoEntityList = joblessInfoService.joblessReduceList(data);
			for (JoblessInfoEntity joblessInfoEntity : joblessInfoEntityList) {
				JoblessInfoData joblessInfoData = new JoblessInfoData();
				//失业人员信息实体类转化为失业人员信息dto
				joblessInfoData.toEntityData(joblessInfoData,joblessInfoEntity);
				resultList.add(joblessInfoData);
			}
			result.setTotal(joblessInfoService.countremovereason(data));
			result.setRows(resultList);
			success= ResultJson.SUCCESS_YES;
			message= ResultJson.MESSAGE;
		} catch (Exception e) {
			success= ResultJson.SUCCESS_NO;
			message= "获取失败";
			e.printStackTrace();
		}finally {
			result.setSuccess(success);
			result.setMessage(message);
		}
		return result;
	}
	
	/**
	 * 获取放弃求职人员减少申报信息
	 * @return
	 */
	@RequestMapping(value="/getJoblessGiveupList", method = RequestMethod.POST)
	@ResponseBody
	public ResultJson getJoblessGiveupList(JoblessSearchData data){
		ResultJson result = new ResultJson();
		String success="";
		String message="";
		List<JoblessInfoData> resultList = new ArrayList<JoblessInfoData>();
		List<JoblessInfoEntity> joblessInfoEntityList = new ArrayList<JoblessInfoEntity>();
		try {
			joblessInfoEntityList = joblessInfoService.joblessGiveupList(data);
			for (JoblessInfoEntity joblessInfoEntity : joblessInfoEntityList) {
				JoblessInfoData joblessInfoData = new JoblessInfoData();
				//失业人员信息实体类转化为失业人员信息dto
				joblessInfoData.toEntityData(joblessInfoData,joblessInfoEntity);
				resultList.add(joblessInfoData);
			}
			result.setTotal(joblessInfoService.countgiveup(data));
			result.setRows(resultList);
			success= ResultJson.SUCCESS_YES;
			message= ResultJson.MESSAGE;
		} catch (Exception e) {
			success= ResultJson.SUCCESS_NO;
			message= "获取失败";
			e.printStackTrace();
		}finally {
			result.setSuccess(success);
			result.setMessage(message);
		}
		return result;
	}

	/**
	 * 根据主键对放弃求职人员的申请进行提交操作
	 * @param unemployedId
	 * @return
	 */
	@RequestMapping(value="/auditSubmit", method = RequestMethod.POST)
	@ResponseBody
	public String auditSubmit(@RequestParam("unemployedId") String Id){
		String result = "";
		ResultJson resultJson = new ResultJson(); 
		try {
			joblessInfoService.auditSubmitInfo(Id);
			resultJson.setSuccess(ResultJson.SUCCESS_YES);
			resultJson.setMessage("审核通过");
		} catch (Exception e) {
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
			resultJson.setMessage("审核失败");
			e.printStackTrace();
		}finally{
			try {
				result = mapper.writeValueAsString(resultJson);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	/**
	 * 根据主键对放弃求职人员的申请进行核准操作
	 * @param unemployedId
	 * @return
	 */
	@RequestMapping(value="/auditGiveup", method = RequestMethod.POST)
	@ResponseBody
	public String auditGiveup(@RequestParam("unemployedId") String Id){
		String result = "";
		ResultJson resultJson = new ResultJson(); 
		try {
			joblessInfoService.auditGiveupInfo(Id);
			resultJson.setSuccess(ResultJson.SUCCESS_YES);
			resultJson.setMessage("审核通过");
		} catch (Exception e) {
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
			resultJson.setMessage("审核失败");
			e.printStackTrace();
		}finally{
			try {
				result = mapper.writeValueAsString(resultJson);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	/**
	 * 根据主键对放弃求职人员进行删除（清空“减少原因”、“放弃求职审核”、“减少时间”）
	 * @param unemployedId
	 * @return
	 */
	@RequestMapping(value="/giveupDel", method = RequestMethod.POST)
	@ResponseBody
	public String giveupDel(@RequestParam("unemployedId") String Id){
		String result = "";
		ResultJson resultJson = new ResultJson(); 
		try {
			joblessInfoService.giveupDel(Id);
			resultJson.setSuccess(ResultJson.SUCCESS_YES);
			resultJson.setMessage("审核通过");
		} catch (Exception e) {
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
			resultJson.setMessage("审核失败");
			e.printStackTrace();
		}finally{
			try {
				result = mapper.writeValueAsString(resultJson);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
}
